"""
实现切片对象
"""

import numbers


class Group():
    """
    实现collections.abc.Sequence中的部分魔法函数，支持切片操作
    """

    def __init__(self, name, company_name, staffs):
        self.name = name
        self.company_name = company_name
        self.staffs = staffs

    def __reversed__(self):
        return reversed(self.staffs)

    def __getitem__(self, item):
        """
        返回可切片对象
        """
        cls = type(self)
        if isinstance(item, slice):
            return cls(name=self.name, company_name=self.company_name, staffs=self.staffs[item])
        elif isinstance(item, numbers.Integral):
            return cls(name=self.name, company_name=self.company_name, staffs=[self.staffs[item]])

    def __len__(self):
        return len(self.staffs)

    def __iter__(self):
        return iter(self.staffs)

    def __contains__(self, item):
        if item in self.staffs:
            return True
        else:
            return False


if __name__ == '__main__':
    group = Group(name="集团名称", company_name="公司名称", staffs=["Kevin", "葛小伦", "天使彦"])
    print(len(group))
    if "Kevin" in group:
        print("找到 Kevin")
    sub_group = group[:2]
    print(sub_group.staffs)
    sub_group = group[0]
    print(sub_group.staffs)
    # 输出结果：
    """
    3
    找到 Kevin
    ['Kevin', '葛小伦']
    ['Kevin']
    """
